package com.xixiake.activity.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TabHost;

import com.xixiake.R;
import com.xixiake.activity.MainActivity;
import com.xixiake.common.AppSetting;
import com.xixiake.own_v4.Fragment;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
/**
 * Created by c-wanghengli on 2017/4/4.
 */
public class FrameFragment extends Fragment implements TabHost.TabContentFactory,TabHost.OnTabChangeListener {

    private TabHost mTabHost;
    private Map<String,Frame> mFrames = new HashMap<>();
    private String tabName;

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View mainView = inflater.inflate(R.layout.activity_main,null);
        return mainView;
    }

    @Override
    public void onResume() {
        super.onResume();
        Frame frame = getCurrentFrame();
        if(frame != null){
            frame.onResume();
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        Frame frame = getCurrentFrame();
        if(frame != null){
            frame.onPause();
        }

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Collection<Frame> collection = mFrames.values();
        for(Frame frame : collection){
            frame.onDestory();
        }
    }

    protected void addFrame(View contentView, Class<? extends Frame> clz, View tab){
        if (mTabHost == null){
            mTabHost = (TabHost) contentView.findViewById(android.R.id.tabhost);
            mTabHost.setup();
            mTabHost.setOnTabChangedListener(this);
        }

        if(null != mTabHost && null != clz && null != tab){
            mTabHost.addTab(mTabHost.newTabSpec(clz.getName()).setIndicator(tab).setContent(this));
        }

    }

    public void setCurrentTab(int i) {
         if(mTabHost != null){
            mTabHost.setCurrentTab(i);
         }
    }

    public int getCurrentTab(){
        if(mTabHost != null){
            return mTabHost.getCurrentTab();
        }
        return 0;
    }

    public Frame getCurrentFrame(){
        if(mTabHost != null){
            return mFrames.get(mTabHost.getCurrentTabTag());
        }
        return null;
    }

    @Override
    public void onTabChanged(String tagId) {
         /*if(tagId != null){
             int index = tagId.lastIndexOf(".");
             if(index >= 0) {
                 tabName = tagId.substring(index + 1, tagId.length());
             }
         }*/
    }

    @Override
    public View createTabContent(String tag) {
        if(AppSetting.isLoggerDebugVersion){
           // Logger.d("tag:s%",tag);
        }
        Frame frame = null;
        View contentView = null;
        try {
            final String className = tag;
            try {
                frame = (Frame) Class.forName(tag).newInstance();
                frame.setActivity((MainActivity) getActivity());
                contentView = frame.onCreateView(getActivity().getLayoutInflater());
                frame.setContentView(contentView);
                frame.onCreate();
                mFrames.put(className,frame);
            } catch (java.lang.InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        return contentView;
    }
}
